package com.hgxd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hgxd.annotation.OperationLogAnnotation;
import com.hgxd.entity.pojo.Slave;
import com.hgxd.entity.pojo.SlaveHistory;
import com.hgxd.entity.req.ReqSlaveGroup;
import com.hgxd.result.Result;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

import com.hgxd.service.ISlaveLinkGroupService;
import com.hgxd.entity.pojo.SlaveLinkGroup;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 从机和从机分组关联表 前端控制器
 * </p>
 *
 * @author wtf
 * @since 2024-09-02
 */
@Api(tags = "从机和从机分组关联表管理")
@RestController
@RequestMapping("/api/slave-link-group")
public class SlaveLinkGroupController {
    @Resource(name = "SlaveLinkGroupServiceImpl")
    private ISlaveLinkGroupService slaveLinkGroupService;

    /**
     * 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增", notes = "在原有的基础上新增关联关系")
    @OperationLogAnnotation(operModule = "从机和从机分组关联表管理-增加电表分组", operType = "新增", operContent = "增加从机分组")
    public Result<Boolean> insert(@RequestBody ReqSlaveGroup slaveGroup) {
        return Result.ok(slaveLinkGroupService.saveSlaveGroups(slaveGroup));
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改", notes = "此接口底层逻辑是先删除所有有关某slaveGroupId的关系，然后在添加，本质是覆盖")
    @OperationLogAnnotation(operModule = "从机和从机分组关联表管理-修改电表分组", operType = "修改", operContent = "修改从机分组")
    public Result<Boolean> update(@RequestBody ReqSlaveGroup slaveGroup) {
        return Result.ok(slaveLinkGroupService.updateSlaveGroups(slaveGroup));
    }

    /**
     * 根据slaveGroupId删除
     */
    @PostMapping("/delete/{slaveGroupId}")
    @ApiOperation(value = "根据slaveGroupId删除", notes = "必须传入slaveGroupId")
    @OperationLogAnnotation(operModule = "从机和从机分组关联表管理-删除电表分组", operType = "删除", operContent = "根据slaveGroupId删除从机分组")
    public Result<Boolean> delete(@PathVariable Integer slaveGroupId) {
        return Result.ok(slaveLinkGroupService.deleteBySlaveGroupsId(slaveGroupId));
    }

    /**
     * 根据slaveId删除
     */
    @PostMapping("/deleteBySlaveId/{slaveId}")
    @ApiOperation(value = "根据slaveId删除", notes = "必须传入slaveId")
    @OperationLogAnnotation(operModule = "从机和从机分组关联表管理-删除电表分组", operType = "删除", operContent = "根据slaveId删除从机分组关系")
    public Result<Boolean> deleteBySlaveId(@PathVariable Integer slaveId) {
        return Result.ok(slaveLinkGroupService.deleteBySlaveId(slaveId));
    }


    /**
     * 查询全部
     */
    @GetMapping("/getAll")
    @ApiOperation(value = "查询全部", notes = "无需传参")
    @OperationLogAnnotation(operModule = "从机和从机分组关联表管理-查询全部电表分组", operType = "查询", operContent = "查询全部从机分组")
    public Result<List<SlaveLinkGroup>> findAll() {
        return Result.ok(slaveLinkGroupService.list());
    }

    /**
     * 根据groupId查询其中的电表最新数据
     */
    @GetMapping("getBySlaveGroupId/{slaveGroupId}")
    @ApiOperation(value = "根据slaveGroupId查询其中的电表最新数据", notes = "传入slaveGroupId")
    @OperationLogAnnotation(operModule = "从机和从机分组关联表管理-根据groupId查询其中的电表最新数据", operType = "查询", operContent = "根据groupId查询其中的电表最新数据")
    public Result<List<SlaveHistory>> getSlaveMessageByGroupId(@PathVariable Integer slaveGroupId) {
        return Result.ok(slaveLinkGroupService.getSlaveMessageByGroupId(slaveGroupId));
    }

    /**
     * 根据slaveGroupId查询其中的电表id
     * @param slaveGroupId
     * @return
     */
    @GetMapping("getSlaveIdBySlaveGroupId/{slaveGroupId}")
    @ApiOperation(value = "根据slaveGroupId查询其中的电表id", notes = "传入slaveGroupId")
    @OperationLogAnnotation(operModule = "从机和从机分组关联表管理-根据slaveGroupId查询其中的电表id", operType = "查询", operContent = "根据slaveGroupId查询其中的电表id")
    public Result<List<Integer>> getSlaveIdBySlaveGroupId(@PathVariable Integer slaveGroupId) {

        LambdaQueryWrapper<SlaveLinkGroup> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SlaveLinkGroup::getSlaveGroupId,slaveGroupId);
        List<Integer> list = slaveLinkGroupService.list(queryWrapper)
                .stream().map(SlaveLinkGroup::getSlaveId)
                .collect(Collectors.toList());

        return Result.ok(list);
    }


}

